#include "stdafx.h"
#include "Registry.h"

Registry::Registry(HKEY hkey,PTSTR subKey){
	//uint e=RegOpenKeyEx(hkey,subKey,0,KEY_READ,&key);
	uint e=RegCreateKeyEx(hkey,subKey,0,0,0,KEY_READ|KEY_WRITE,0,&key,0);
	if(e!=ERROR_SUCCESS)
		throw(e);
}
Registry::~Registry	(void){
	RegCloseKey(key);
}

void Registry::del(PTSTR name){
	RegDeleteValue(key,name);
}
uint Registry::get(PTSTR name,int defVal){
	uint res;
	uint n=sizeof(res);
	uint e=RegQueryValueEx(key,name,0,0,(LPBYTE)&res,&n);
	if(e==ERROR_SUCCESS)
		return res;
	throw(0);
}
String* Registry::get(PTSTR name,PTSTR defVal){
	uint n;
	uint e=RegQueryValueEx(key,name,0,0,0,&n);
	if(e!=ERROR_SUCCESS)
		if(defVal)
			return new String(defVal);
		else throw(0);
	String* res=new String(n/2);
	e=RegQueryValueEx(key,name,0,0,(LPBYTE)res->Ptstr(),&n);
	if(e!=ERROR_SUCCESS)
		throw(0);
	return res;
}
void Registry::get(PTSTR name,PTSTR buffer,uint length){
	uint e=RegQueryValueEx(key,name,0,0,(LPBYTE)buffer,&length);
	if(e!=ERROR_SUCCESS)
		throw(0);
}
void Registry::set(PTSTR name,PTSTR value){
	RegSetValueEx(key,name,0,REG_SZ,(const BYTE*)value,
		_tcslen(value)*sizeof(TCHAR));
}

MachineOrUser::MachineOrUser(PTSTR subKey){
	if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,subKey,0,KEY_READ,&key)==ERROR_SUCCESS)
		return;
	uint e=RegOpenKeyEx(HKEY_CURRENT_USER,subKey,0,KEY_READ,&key);
	if(e!=ERROR_SUCCESS)
		throw(0);
};
